# Django settings
import os
from pathlib import Path
import json

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'channels',
    'corsheaders',
    'ai',  # 使用应用名称而不是相对路径
]

# 文件上传设置
DATA_UPLOAD_MAX_MEMORY_SIZE = 20 * 1024 * 1024  # 20MB
FILE_UPLOAD_MAX_MEMORY_SIZE = 20 * 1024 * 1024  # 20MB
DATA_UPLOAD_MAX_NUMBER_FIELDS = 10000

# 配置 Channels
ASGI_APPLICATION = 'ai.asgi.application'

# CORS设置
CORS_ALLOW_ALL_ORIGINS = True  # 开发环境下允许所有源
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = [
    "http://localhost:8080",
    "http://127.0.0.1:8080",
    "http://localhost:3000",
    "http://127.0.0.1:3000",
]
CORS_ALLOW_METHODS = [
    'GET',
    'POST',
    'OPTIONS',
]
CORS_ALLOW_HEADERS = [
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-requested-with',
    'x-csrftoken',
]

# 星火大模型配置
SPARKAI_CONFIG = {
    'URL': 'wss://spark-api.xf-yun.com/v4.0/chat',
    'APP_ID': '9755e82a',
    'API_SECRET': 'OWU4NGNmMjkxNGZmMWU0MjI5ZjJlZDI1',
    'API_KEY': '5a514de5792351ee05593443e7f13606',
    'DOMAIN': '4.0Ultra'  # 修改为4.0版本的模型
}

# 千问配置
QWEN_CONFIG = {
    'API_KEY': 'sk-511def88ec304938818b103963ff046e',  # 替换为您的通义千问 API Key
    'MODEL': 'qwen-turbo',  # 使用千问通用模型
    'API_BASE': 'https://dashscope.aliyuncs.com',
    'DEFAULT_PARAMS': {
        'temperature': 0.7,
        'top_p': 0.8,
        'max_tokens': 1500,
        'result_format': 'message'
    }
}

# Channels Layer 配置
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
    }
}

# 语音识别配置
SPEECH_RECOGNITION_CONFIG = {
    'MODEL': 'paraformer-realtime-v2',
    'FORMAT': 'pcm',
    'SAMPLE_RATE': 16000,
    'CHUNK_SIZE': 4096,
    'CHANNELS': 1,
    'API_KEY': 'sk-511def88ec304938818b103963ff046e'  # 添加 sk- 前缀
}

# 基础 Django 设置
SECRET_KEY = 'django-insecure-your-secret-key-here'
DEBUG = True
ALLOWED_HOSTS = ['*']

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',  # CORS 中间件必须放在最前面
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',  # 暂时注释掉 CSRF 中间件以便测试
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
]

ROOT_URLCONF = 'ai.urls'  # 修改为使用根 URL 配置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# 数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# 日志配置
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO',
    },
}